﻿// Decompiled with JetBrains decompiler
// Type: FlamethrowerSpell.FlamethrowerSpell
// Assembly: FlamethrowerSpell, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: FE877AF4-189C-4F20-BE78-CAD168BEA322
// Assembly location: E:\Temp\Flamethrower Spell\FlamethrowerSpell.dll

using System.Collections.Generic;
using ThunderRoad;
using UnityEngine;

namespace FlamethrowerSpell
{
  public class FlamethrowerSpell : SpellCastCharge
  {
    private EffectData effect;
    private EffectInstance instance;
    private StatusDataBurning burning;
    private float lastTick = Time.time;
    public HashSet<(ThunderEntity entity, UnityEngine.Vector3 point)> entities = new HashSet<(ThunderEntity, UnityEngine.Vector3)>();

    public override void Load(SpellCaster spellCaster)
    {
      base.Load(spellCaster);
      this.effect = Catalog.GetData<EffectData>("Flamethrower");
      this.burning = Catalog.GetData<StatusDataBurning>("Burning");
      if (!spellCaster.mana.creature.isPlayer)
      {
        spellCaster.fire.rotation = spellCaster.magicSource.rotation * Quaternion.Euler(-70f, 0.0f, 0.0f);
        this.sprayHeadToFireMaxAngle = 45f;
      }
      else
        spellCaster.fire.rotation = spellCaster.magicSource.rotation;
    }

    public override void Unload()
    {
      base.Unload();
      this.instance?.End();
      this.entities.Clear();
      if (!this.spellCaster.mana.creature.isPlayer)
        return;
      Player.local.physicRangeModifier.Remove((object) this);
    }

    public override void Fire(bool active)
    {
      base.Fire(active);
      if (Mana.fastCast & active)
      {
        this.instance?.End();
        this.instance = this.effect.Spawn(this.spellCaster.fire, (CollisionInstance) null, true, (ColliderGroup) null, false, 1f, 1f);
        this.instance.SetIntensity(1f);
        this.instance.Play();
        if (this.spellCaster.mana.creature.isPlayer)
        {
          Player.local.physicRangeModifier.Add((object) this, 6f * FlamethrowerManager.Scale * this.GetModifier(Modifier.Range));
          foreach (ThunderBehaviour effect in this.instance.effects)
            effect.transform.localScale = UnityEngine.Vector3.one * 0.5f * FlamethrowerManager.Scale;
        }
      }
      if (active)
        return;
      this.instance?.End();
      this.entities.Clear();
      if (this.spellCaster.mana.creature.isPlayer)
        Player.local.physicRangeModifier.Remove((object) this);
    }

    public override void FixedUpdateCaster()
    {
      base.FixedUpdateCaster();
      if (!this.isSpraying || !this.spellCaster.mana.creature.isPlayer)
        return;
      this.spellCaster.ragdollHand?.physicBody?.AddForce(-this.spellCaster.fire.forward * FlamethrowerManager.PlayerForce * this.spellCaster.fireAxis, ForceMode.Acceleration);
    }

    public override void OnSprayStart()
    {
      this.instance?.End();
      this.instance = this.effect.Spawn(this.spellCaster.fire, (CollisionInstance) null, true, (ColliderGroup) null, false, 1f, 1f);
      this.instance.SetIntensity(1f);
      this.instance.Play();
      if (!this.spellCaster.mana.creature.isPlayer)
        return;
      Player.local.physicRangeModifier.Add((object) this, 6f * FlamethrowerManager.Scale * this.GetModifier(Modifier.Range));
      foreach (ThunderBehaviour effect in this.instance.effects)
        effect.transform.localScale = UnityEngine.Vector3.one * 0.5f * FlamethrowerManager.Scale;
    }

    public override void OnSprayStop()
    {
      this.instance?.End();
      this.entities.Clear();
      if (!this.spellCaster.mana.creature.isPlayer)
        return;
      Player.local.physicRangeModifier.Remove((object) this);
    }

    public override void OnSprayLoop()
    {
      if (this.spellCaster.mana.creature.isPlayer && (Object) this.spellCaster.mana.GetCaster(this.spellCaster.side) == (Object) this.spellCaster)
        this.spellCaster.fire.rotation = this.spellCaster.magicSource.rotation * Quaternion.Euler(-FlamethrowerManager.FireDirectionAngle, 0.0f, 0.0f);
      if (this.spellCaster.mana.creature.isPlayer)
      {
        foreach (ThunderBehaviour effect in this.instance.effects)
          effect.transform.localScale = UnityEngine.Vector3.one * 0.5f * FlamethrowerManager.Scale;
      }
      if ((double) Time.time - (double) this.lastTick < 0.30000001192092896)
        return;
      this.lastTick = Time.time;
      float num1 = 6f * FlamethrowerManager.Scale;
      RaycastHit hitInfo;
      if (Physics.Raycast(new Ray(this.spellCaster.fire.position, this.spellCaster.fire.forward), out hitInfo, 6f * FlamethrowerManager.Scale, 5201, QueryTriggerInteraction.Ignore))
        num1 = hitInfo.distance;
      float num2 = (float) ((double) num1 * 1.0 / 5.0);
      float radius1 = (float) ((double) num1 * 1.0 / 5.0);
      float num3 = (float) ((double) num1 * 3.0 / 5.0);
      float radius2 = (float) ((double) num1 * 2.0 / 5.0);
      this.entities.Clear();
      ThunderEntity.InRadiusClosestPoint(this.spellCaster.fire.position + this.spellCaster.fire.forward * num2, radius1, allocList: (ICollection<(ThunderEntity, UnityEngine.Vector3)>) this.entities);
      ThunderEntity.InRadiusClosestPoint(this.spellCaster.fire.position + this.spellCaster.fire.forward * num3, radius2, allocList: (ICollection<(ThunderEntity, UnityEngine.Vector3)>) this.entities);
      foreach ((ThunderEntity entity, UnityEngine.Vector3 point) in this.entities)
      {
        if ((Object) entity != (Object) this.spellCaster.mana.creature)
        {
          ThunderEntity thunderEntity = entity;
          UnityEngine.Vector3 vector3 = point - this.spellCaster.fire.position;
          UnityEngine.Vector3 force = vector3.normalized * (this.spellCaster.mana.creature.isPlayer ? FlamethrowerManager.Force : 10f);
          thunderEntity.AddForce(force, ForceMode.Acceleration);
          entity.Inflict((StatusData) this.burning, (object) this, this.spellCaster.mana.creature.isPlayer ? float.PositiveInfinity : 5f, (object) (float) ((this.spellCaster.mana.creature.isPlayer ? 70.0 : 20.0) * 0.30000001192092896));
          Creature creature1 = entity as Creature;
          if ((Object) creature1 != (Object) null)
          {
            if (!entity.IsBurning)
            {
              Creature creature2 = creature1;
              vector3 = point - this.spellCaster.fire.position;
              UnityEngine.Vector3 direction = vector3.normalized * (this.spellCaster.mana.creature.isPlayer ? FlamethrowerManager.Force : 10f);
              creature2.TryPush(Creature.PushType.Magic, direction, 1);
            }
            creature1.Damage(this.spellCaster.mana.creature.isPlayer ? FlamethrowerManager.Damage : 1f, DamageType.Fire);
          }
        }
      }
    }
  }
}
